package cn.iocoder.yudao.module.ao.service.subjectgroup;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.subjectgroup.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.subjectgroup.SubjectGroupDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.subjectgroup.SubjectGroupMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 学科组信息 Service 实现类
 *
 * @author 智教云枢
 */
@Service
@Validated
public class SubjectGroupServiceImpl implements SubjectGroupService {

    @Resource
    private SubjectGroupMapper subjectGroupMapper;

    @Override
    public Long createSubjectGroup(SubjectGroupSaveReqVO createReqVO) {
        // 插入
        SubjectGroupDO subjectGroup = BeanUtils.toBean(createReqVO, SubjectGroupDO.class);
        subjectGroupMapper.insert(subjectGroup);

        // 返回
        return subjectGroup.getGroupId();
    }

    @Override
    public void updateSubjectGroup(SubjectGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateSubjectGroupExists(updateReqVO.getGroupId());

        // 更新
        SubjectGroupDO updateObj = BeanUtils.toBean(updateReqVO, SubjectGroupDO.class);
        subjectGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubjectGroup(Long id) {
        // 校验存在
        validateSubjectGroupExists(id);
        // 删除
        subjectGroupMapper.deleteById(id);
    }

    @Override
        public void deleteSubjectGroupListByIds(List<Long> ids) {
        // 删除
        subjectGroupMapper.deleteByIds(ids);
        }


    private void validateSubjectGroupExists(Long id) {
        if (subjectGroupMapper.selectById(id) == null) {
            throw exception(SUBJECT_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public List<SubjectGroupDO> getSimpleSubjectGroupList() {
        return subjectGroupMapper.selectList();
    }



    @Override
    public SubjectGroupDO getSubjectGroup(Long id) {
        return subjectGroupMapper.selectById(id);
    }

    @Override
    public PageResult<SubjectGroupDO> getSubjectGroupPage(SubjectGroupPageReqVO pageReqVO) {
        return subjectGroupMapper.selectPage(pageReqVO);
    }

}